﻿


/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 12/14/2010
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoCountry
    {
        public daoCountry()
        {

        }
        public CountryCollection GetAll()
        {
            clsSQL clsSQL = new clsSQL();         
            SqlDataReader de;
            CountryCollection items = new CountryCollection();
            try
            {
            
                de = clsSQL.SPDataReader("Country_GetAll");
                while (de.Read())
                {
                    Country item = Load(de);
                    items.Add(item);
                }

                de.Close();              
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoCountry", "GetAll()", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }


        public Country GetById(int id)
        {
            clsSQL clsSQL = new clsSQL();
            SqlDataReader de;
            Country item = new Country();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCountry.COUNTRYID, SqlDbType.Int);
                para[0].Value = id;
                de = clsSQL.SPDataReader("Country_GetById",para);
                if (de.Read())
                {
                    item = Load(de);                   
                }

                de.Close();
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoCountry", "GetById(int id)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return item;
        }

        private Country Load(IDataReader de)
        {
            Country item = new Country();
            item.CountryID = clsConvert.DbToInt("CountryId", de);
            item.CountryCode = clsConvert.DbToString("CountryCode", de);
            item.CountryName = clsConvert.DbToString("CountryName", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);      
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }



        /// <summary>
        /// Move item up
        /// </summary>
        /// <param name="countryId"></param>
        /// <returns></returns>
        public bool OrderUp(int countryId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCountry.COUNTRYID, SqlDbType.Int);
                para[0].Value = countryId;
                result = clsSQL.SPExecute("Country_OrderUp", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCountry", "OrderUp(int countryId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        /// <summary>
        /// Move item down
        /// </summary>
        /// <param name="CountryID"></param>
        /// <returns></returns>
        public bool OrderDown(int CountryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCountry.COUNTRYID, SqlDbType.Int);
                para[0].Value = CountryID;
                result = clsSQL.SPExecute("Country_OrderDown", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCountry", "OrderDown(int CountryID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Active(int CountryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCountry.COUNTRYID, SqlDbType.Int);
                para[0].Value = CountryID;
                result = clsSQL.SPExecute("Country_Active", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCountry", "Active(int CountryID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool DeActive(int CountryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCountry.COUNTRYID, SqlDbType.Int);
                para[0].Value = CountryID;
                result = clsSQL.SPExecute("Country_DeActive", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCountry", "DeActive(int CountryID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int CountryID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCountry.COUNTRYID, SqlDbType.Int);
                para[0].Value = CountryID;
                result = clsSQL.SPExecute("Country_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCountry", "Delete(int CountryID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public int Insert(Country objCountry)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[3];
            try
            {
                para[0] = new SqlParameter(busCountry.COUNTRYCODE, SqlDbType.VarChar);
                para[0].Value = clsConvert.StringToDB(objCountry.CountryCode);

                para[1] = new SqlParameter(busCountry.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objCountry.IsActive;

                para[2] = new SqlParameter(busCountry.COUNTRYNAME, SqlDbType.NVarChar);
                para[2].Value = objCountry.CountryName;

                result = clsSQL.SPExecute("Country_Insert", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCountry", "Country_Insert(Country Country)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public int Update(Country objCountry)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[4];
            try
            {
                para[0] = new SqlParameter(busCountry.COUNTRYCODE, SqlDbType.VarChar);
                para[0].Value = clsConvert.StringToDB(objCountry.CountryCode);

                para[1] = new SqlParameter(busCountry.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objCountry.IsActive;

                para[2] = new SqlParameter(busCountry.COUNTRYNAME, SqlDbType.NVarChar);
                para[2].Value = objCountry.CountryName;
                para[3] = new SqlParameter(busCountry.COUNTRYID, SqlDbType.Int);
                para[3].Value = objCountry.CountryID;
                result = clsSQL.SPExecute("Country_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCountry", "Update(Country objCountry)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }           
            return result;
        }
        public CountryCollection List(Country country)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            CountryCollection items = new CountryCollection();

            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = country.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = country.OrderDirection;
                para[2] = new SqlParameter(busCategory.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = country.CurrentPage;
                para[3] = new SqlParameter(busCategory.PAGESIZE, SqlDbType.Int);
                para[3].Value = country.PageSize;
                para[4] = new SqlParameter(busCategory.CONDITION, SqlDbType.NVarChar);
                para[4].Value = country.Condition;
                para[5] = new SqlParameter(busCategory.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Country_List", para);
                while (de.Read())
                {
                    Country item = Load(de);
                    items.Add(item);
                }
                de.Close();
                country.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoCategory", "List(Country country)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }
    }
}

